if (typeof (data) == "string") {
	data = JSON.parse(data);
}

var Controller = {
	initBootTable : function() {
		$('#proprietorListTable').bootstrapTable({
			cache : false,
			filterControl : true,
			filterShowClear : true,
			alignmentSelectControlOptions : "left",
			stickyHeader : true,
			pagination : true,
			pageSize : 10,
			pageNumber : 1,
			pageList : [ 5, 10, 20, 50, 100 ],
			uniqueId : "ID",
			search : true,
			clickToSelect : false,
			sortName : 'houseNum',
			columns : [ {
				field : "pid",
				title : "业主编号",
				align : "center",
				valign : "middle",
				width : '15%',
				sortable : "true"
			}, {
				field : "pName",
				title : "业主名称",
				align : "center",
				valign : "middle",
				width : '10%',
				sortable : "true"
			}, {
				field : "mobile",
				title : "手机号",
				align : "center",
				valign : "middle",
				width : '10%',
				sortable : "true"
			}, {
				field : "houseNum",
				title : "房号",
				align : "center",
				valign : "middle",
				width : '15%',
				sortable : "true"
			}, {
				field : "operation",
				title : "操作",
				align : "center",
				valign : "middle",
				width : '15%'
			} ],
			data : data.dataSource,
			formatNoMatches : function() {
				return '没有记录！';
			}
		});
	}
}

$(document).ready(function() {
	Controller.initBootTable();
});

function returnComm() {
	var url = null;
	url = "/IMS/community/showList.ht";
	url += "?cid=" + cid_;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

/*
 * function returnComm1() { var url = null; if (type != "3") { url = "/IMS/community/showList.ht"; url = url + "?companyId=" + pcid; } else { url = "/IMS/community/showList.ht"; } $.go(url, { dataType : 'html', beforeSend : function() { showProccessBar() }, error : function() { hideProccessBar(); }, success : function(data, textStatus) { replaceDocument(url, data); } }); }
 */

var searchBatchInterval;
var alert_index;

function importExcel() {
	var excelFile = $("#excelfile").val();
	if (excelFile == '') {
		_alert("请选择需要上传的文件!");
		return false;
	}
	if (excelFile.indexOf('.xls') == -1) {
		_alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)！");
		return false;
	}
	$.submit($('#excelfrm'), {
		dataType : 'json',
		enctype : 'multipart/form-data',
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			var code = data.code;
			var message = data.message;
			if (code == 0) {
				$("#batchNum").val(message);
				alert_index = alertMsg('', '正在处理,请勿关闭页面', 4);
				searchBatchInterval = self.setInterval("searchValidatorResult()", 15000);
			} else if (code == -1 || code == -2) {
				alertMsg('', message, 5);
			}
			hideProccessBar();
		}
	});
}

function searchValidatorResult() {
	var batchNum = $("#batchNum").val();
	$.ajax({
		type : "POST",
		url : "/IMS/pdsHousePrietor/search.ht",
		data : {
			batchNum : batchNum
		},
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, status) {
			clearInterval(searchBatchInterval);
			layer.close(alert_index);
			var dataCode = data.code;
			if (dataCode == 0) {
				setValidatorResult(data);
			}
			hideProccessBar();
		},
	});
}

function setValidatorResult(data) {
	if (data.result != null) {
		var rvrDtoList = data.result.rvrDtoList;
		var isSuccess = data.result.isSuccess;
		var message = data.message;

		if (isSuccess == "err") {
			var errorRowSize = rvrDtoList.length;
			$("#importHistoricalPrepaidResultDiv").show();
			$("#importHistoricalPrepaidResultTab tr:not(:first)").empty();
			for (var i = 0; i < errorRowSize; i++) {
				var errorRow = rvrDtoList[i];
				// 行号
				var rowNum = errorRow.rowMarker;
				rowNum = parseInt(rowNum);
				if (!isNaN(rowNum)) {
					rowNum = rowNum + 1;
				}

				var newRow = "<tr><td align='center'>" + rowNum + "</td><td>" + errorRow.errorMsg + "</td></tr>";
				$("#importHistoricalPrepaidResultTab tr:last").after(newRow);
			}
			$("#allImportCount").html(data.result.totalCount);
			$("#successImportCount").html(data.result.totalCount - errorRowSize);
			$("#failedImportCount").html(errorRowSize);
		} else if (isSuccess == "fail") {
			alertMsg('错误提示', message, 5);
		} else {
			$("#importHistoricalPrepaidResultDiv2").show();
			$("#allImportCount2").html(data.result.totalCount);
			$("#successImportCount2").html(data.result.totalCount);
		}

	}
}

// 编辑页面
function addUser() {
	var url = "/IMS/owner/showBaseInfo.ht?communityId=" + cid_;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 更新
function updateOwner(checkedId) {
	var url = "/IMS/owner/showBaseInfo.ht";
	url = url + "?checkUserId=" + checkedId + "&communityId=" + cid_;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function closeLayer(obj) {
	var listurl = "/IMS/owner/showList.ht?communityId=" + cid_ + "&pcid=" + pcid_;
	$.go(listurl, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(listurl, data);
		}
	});
	$("#" + obj).css("display", "none");
}

function view(checkedId) {
	var url = "/IMS/owner/showBaseInfoView.ht";
	if (!checkedId) {
		return false;
	} else {
		url = url + "?checkUserId=" + checkedId;
		$.go(url, {
			dataType : 'html',
			beforeSend : function() {
				showProccessBar()
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	}
}

// 打开excel页面
function openExcelPage(openType) {
	if (openType == 'export') {
		$("#ks-pop-box2").show();
	} else if (openType == 'import') {
		$("#ks-pop-box3").show();
	}

}
function exp() {
	var controlUrl = "/IMS/owner/exportExcel.ht";
	var url = controlUrl + "?cid=" + cid_;
	$.ajax({
		url : '/IMS/securityProtect/check.ht',
		data : {
			url : controlUrl
		},
		beforeSend : function() {
			showProccessBar()
		},
		error : function() {
			hideProccessBar();
		},
		success : function() {
			hideProccessBar();
			window.open(url);
		}
	});
}

function modifyState(checkedId, custNo, hid) {
	_confirm("<span style=\"color:#FE6600\">删除之后数据不可恢复！是否确认删除？</span>", function() {
		var url = "/IMS/owner/modifyState.ht";
		if (!checkedId) {
			return false;
		} else {
			url = url + "?checkUserId=" + checkedId + "&custNo=" + custNo + "&hid=" + hid;
			$.ajax({
				url : url,
				async : true,
				type : 'post',
				beforeSend : function() {
					showProccessBar()
				},
				error : function() {
					hideProccessBar();
				},
				success : function(data) {
					var code = data.messageCode;
					if (code == message_success_code) {
						_alert(data.message, true, true);
						var listurl = "/IMS/owner/showList.ht?communityId=" + cid_ + "&pcid=" + pcid_;
						$.go(listurl, {
							dataType : 'html',
							beforeSend : function() {
								showProccessBar();
							},
							error : function() {
								hideProccessBar();
							},
							success : function(data, textStatus) {
								replaceDocument(listurl, data);
							}
						});
					} else {
						_alert(message_false);
						hideProccessBar();
					}
				}
			});
		}
	});
}

function showFileName() {
	var fileNameText = $("#excelfile").val();
	fileNameText = fileNameText.substring(fileNameText.lastIndexOf('\\') + 1);
	if (fileNameText != '') {
		$("#showFileNameId").text(fileNameText);
	}
}
